package com.wf.day_0604.test1;

import com.wf.day_0604.test1.exception.DeleteException;
import com.wf.day_0604.test1.exception.InsertException;
import com.wf.day_0604.test1.exception.UpdateException;
import com.wf.day_0604.test1.pojo.Book;
import com.wf.day_0604.test1.pojo.SysManager;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * @author mpk
 */
public class Demo {

    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) throws InsertException {
        SysManager sysManager = new SysManager();

        sysManager.addBook( new Book( "编程", "Java从入门到放弃", 100.00));
        sysManager.addBook( new Book( "编程", "python", 1030.00));
        sysManager.addBook( new Book( "名著", "三国演义", 10230.00));
        sysManager.addBook( new Book( "名著", "红楼", 1342.00));
        sysManager.addBook( new Book( "名著", "西游记", 10324.00));
        sysManager.addBook( new Book( "11", "11", 10324.00));

        while (true) {
            //这是学生管理系统的主界面
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1.查看所有书籍");
            System.out.println("2.添加书籍");
            System.out.println("3.删除书籍");
            System.out.println("4.修改书籍");
            System.out.println("5.退出系统");
            System.out.println("请输入你的选择：");
            //创建键盘录入对象

            int num = sc.nextInt();
            switch (num) {
                case 1:
                    // 查看所有书籍
                    sysManager.showAll();
                    break;
                case 2:
                    // 添加书籍
                    try {
                        Book book = getBook();
                        System.out.println("请输入书籍价格：");
                        double price = sc.nextDouble();
                        book.setPrice(price);
                        sysManager.addBook(book);
                    } catch (InsertException e) {
                        System.out.println(e.getMessage());
                    } catch (InputMismatchException e) {
                        System.out.println("输入价格格式有误！");
                    }
                    break;
                case 3:
                    // 删除书
                    try {
                        sysManager.removeBook(getBook());
                    } catch (DeleteException e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 4:
                    // 修改书
                    try {

                        sysManager.updateBook(getBook());


                    } catch (InsertException | UpdateException | DeleteException e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 5:
                    // 退出
                    sc.close();
                    System.exit(0);
                default:
                    System.out.println("输入错误,请重新输入");
                    break;
            }

        }


    }

    public static Book getBook() {
        System.out.println("请输入书名:");
        sc.nextLine();
        String name = sc.nextLine();
        System.out.println("请输入书籍类型：");
        String type = sc.nextLine();
        return new Book(type, name);
    }
}
